﻿package Naruto.Frames {	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	
	import flash.display.MovieClip; 
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.text.TextFormat;
	import flash.text.TextField;
	
	import fl.transitions.*;
 	import fl.transitions.easing.*;
	
	import Naruto.Bloods.*;
	import Naruto.*;

		
	public class Frame6 extends Frame{
		
		private var _Zabuza:Zabuza;		//再不斩
		private var _BossHealth:Number;	//再不斩生命值
		private var _bossNameField:TextField;	//Boss名字文本
		private var _bossNameFormat:TextFormat;	//Boss名字文本格式
		private var _bossNameContent:String = '桃地再不斩';
		
		private var _skillsHurtMeObj:Array;
		
		private var _aSuYiLiuDan:SuYiLiuDan;	//水龙弹
		private var _SYLDwidth:Number;			//原始长度
		private var _SYLDwidthChange:Boolean = false;	//是否可以长度变化
		
		private var _canMove:Boolean = false;	//是否移动
		private var _trans:TransitionManager;	//触发淡入淡出效果
		//延时用变量
		private const _fps:int = 24;	
		private const _delayTime:int = 2;
		private var _counter:int = 0;
		
		private var creatMyAshTimer:Timer;		//本帧专属计时器
		private var _isMasked:Boolean = false;	//是否已遮罩背景
		public var _MissonResult:MissonResult = null;
		
		public function Frame6(aStatus:Status, stage:Stage, appStart:Start){
			super(aStatus, stage, appStart);
			init();
		}
		private function init():void{
			this._loadPic = new LoadPic(this._stage,6);	//加载第六个场景
			
			addNameText();	//显示Boss名
			this._blood.addBossBlood();
			this._BossHealth = this._Zabuza.getHealth();		//初始化生命值
			this._blood.changeBossHealth(this._BossHealth);		//改变生命值
			
			this._skillsHurtMeObj = new Array();
			
			me.setpos(this._stage,this._stage.stageWidth*(1/2),this._stage.stageHeight*(7/8));				
			me.setLevel(this._status.getLevel());	
			this._stage.addChild(me.getme());	
			
			creatAiAttack();	//再不斩开始攻击
			
			flyshittest.getflystestobj(enemystestobj,me,flystestobj);		//开始飞镖碰撞检测，参数(敌人队列，伤敌飞镖队列，伤我飞镖队列)
			this._stage.addEventListener(Event.ENTER_FRAME,onEnterFrame);
			
			creatMyAshman();		
			
			flag = 0;
			keycode = [0];
			
		}
		
		private function addNameText(){
			this._bossNameFormat = new TextFormat();
			this._bossNameFormat.color = 0xFFFFFF;
			this._bossNameFormat.font = "隶书";
			this._bossNameFormat.size = 24;
			
			this._bossNameField = new TextField();
			this._bossNameField.x = 450;
			this._bossNameField.y = 10;
			this._bossNameField.width = 300;
				
			this._bossNameField.text = this._bossNameContent;
			this._bossNameField.setTextFormat(this._bossNameFormat);
			this._bossNameField.selectable = false;
			this._stage.addChild(this._bossNameField);
		}
		
		override public function creatEnemys():void{
			this._Zabuza = new Zabuza(this._stage, new AZabuza());
			
			CreatBoss();
		}
		
		public function CreatBoss():void{
			this._stage.addChild(this._Zabuza.getme());
			enemystestobj.push(this._Zabuza.getme());	//将再不斩加入碰撞队列
		}
		
		
		//创建BOSS攻击
		override public function creatAiAttack(){
			creatAiAttackTimer = new Timer(2500);
			creatAiAttackTimer.addEventListener(TimerEvent.TIMER,aiAttack);	//敌人开始攻击
			creatAiAttackTimer.start();
		}
				
		override public function aiAttack(event:Event){
			//再不斩攻击分3种：0.飞镖 1.隐身 2.水遁
			var abilityNum = 0;
			
			do{
				abilityNum = Math.round(10*Math.random());
			}while(abilityNum >2)
			
			if(this._Zabuza == null){
				return;
			}
			
			//trace(abilityNum);
			//abilityNum = 2;	//测试水龙弹用
			switch(abilityNum){
				case 0:{
						//敌人攻击
						if(this._Zabuza.getme()){
							if(this._Zabuza.getStatus()){
								this._canMove = true;	//可以移动
								flyshurtmeobj.push(this._Zabuza.getAttack().BossStartAttack(this._stage,this._Zabuza.getme(),me.getme()));	//敌人攻击
							}
						};
						break;}
				case 1:{
						this._canMove = false;
						this._Zabuza.setIsHided(true);
						this._trans = new TransitionManager(this._Zabuza.getme());						
						this._trans.startTransition({type:Fade, direction:Transition.OUT, duration:1});
						this._trans.addEventListener("allTransitionsInDone", a1InDone);	//技能1
						this._trans.addEventListener("allTransitionsOutDone", a1OutDone); //技能1
/*
						if(this._counter >= this._delayTime * this._fps-10){							
							this._counter = 0;
						}else{
							this._counter ++;
						}
*/
						break;}

				case 2:{
						this._canMove = false;
						this._aSuYiLiuDan = new SuYiLiuDan();
						this._SYLDwidth = this._aSuYiLiuDan.width;
						
						this._aSuYiLiuDan.x = this._Zabuza.getme().x;
						this._aSuYiLiuDan.y = this._Zabuza.getme().y;
						var dy = me.getposy() - this._Zabuza.getme().y;
						var dx = me.getposx() - this._Zabuza.getme().x;
						var angle = Math.atan2(dy,dx);
						this._aSuYiLiuDan.rotation = angle * 180 /Math.PI;
						this._stage.addChild(this._aSuYiLiuDan);
						this._skillsHurtMeObj.push(this._aSuYiLiuDan);	//将技能加入技能碰撞队列
						
						this._SYLDwidthChange = true;
						
						this._trans = new TransitionManager(this._aSuYiLiuDan);
						this._trans.startTransition({type:PixelDissolve, direction:Transition.OUT, duration:1, xSections:80, ySections:80});
						this._trans.addEventListener("allTransitionsOutDone", a2OutDone);
						//......
						break;
						}
			}
		}
		private function a1InDone(evt:Event):void{
			//trace("InDone");
			this._trans.removeEventListener("allTransitionsInDone", a1InDone);
			this._stage.addChild(this._Zabuza.getme());
			this._Zabuza.setIsHided(false);
			this._canMove = true;
			flyshurtmeobj.push(this._Zabuza.getAttack().BossStartAttack(this._stage,this._Zabuza.getme(),me.getme()));	//扔个飞镖
		}
		private function a1OutDone(evt:Event):void{
			//trace("OutDone");
			this._trans.removeEventListener("allTransitionsOutDone", a1OutDone);
			this._stage.removeChild(this._Zabuza.getme());
			this._Zabuza.getme().x = Math.round(Math.random()*1000);
			this._Zabuza.getme().y = Math.round(Math.random()*820);
			this._trans.startTransition({type:Fade, direction:Transition.IN, duration:1});
		}
		private function a2OutDone(evt:Event):void{
			this._trans.removeEventListener("allTransitionsOutDone", a2OutDone);	//技能1
			this._SYLDwidthChange = false;
			this._canMove = true;
			if(this._stage.contains(this._aSuYiLiuDan))
				this._stage.removeChild(this._aSuYiLiuDan);
			this._skillsHurtMeObj.pop();	//褪出技能
		}
		
		private function creatMyAshman(){
			creatMyAshTimer = new Timer(1000);
			creatMyAshTimer.addEventListener(TimerEvent.TIMER,myAshman);
			creatMyAshTimer.start();
		}
		
		//本帧专属清道夫
		private function myAshman(event:TimerEvent){		
			
			//移除死亡者(Boss),将Boss在碰撞队列enemystestobj的位置置空
			var allClear = true;
			for(var i=0;i<enemystestobj.length;i++){
				if(enemystestobj[i]!=null){
					allClear = false;
					if(this._Zabuza.getStatus()==0){	//0:最高级别伤害（死亡）
						this._Zabuza.removeBoss();
						enemystestobj[i] = null;	//从碰撞队列移除
						this._Zabuza = null;
					}
				}
			}
			this._isEnd = allClear;
			
			if(this.isAshManStart == true){	
				if(this._isEnd == true){
					this.flag = 1;	//鼠标发镖事件停止。
	//				trace("ash");
					//移除所有监听器
					creatMyAshTimer.removeEventListener(TimerEvent.TIMER,Ashman);
					creatMyAshTimer.stop();
					//creatMyAshTimer = null;
					
					creatAiAttackTimer.removeEventListener(TimerEvent.TIMER,aiAttack);
					creatAiAttackTimer.stop();
					creatAiAttackTimer = null;
					this._stage.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
	//				this._stage.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
					this._stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyboardEvent);
					this._stage.removeEventListener(KeyboardEvent.KEY_UP,onKeyboardEvent);
								
					//除背景移除所有
					var len:uint = this._stage.numChildren;
					while(len>1) {
						this._stage.removeChildAt(len-1);
						len--;
					}
					this._Zabuza = null;
					this.me = null;
					
					
					//保存角色状态
					//trace(this._blood.getHealth());
					this._status.setHealth(this._blood.getHealth());
					this._status.setCkl(this._blood.getCkl());				
					
					if(this._isMasked == false){
						this._loadPic.startMask();
						this._isMasked = true;
					}
					this._MissonResult = new MissonResult(this._stage,6,true,changeFrame);
	//				changeFrame();
				}				
			}
		}
		
		override public function changeFrame(changeNum:int = 99){
			this.flag = 1;	//鼠标发镖事件停止。
			//游戏失败时，移除所有
			this._stage.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			this._stage.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			creatMyAshTimer.removeEventListener(TimerEvent.TIMER,Ashman);
			creatMyAshTimer.stop();
			creatMyAshTimer = null;
			this._Zabuza = null;
			switchChangeNum(changeNum,6);
		}
		
		override public function onEnterFrame(event:Event):void{
			if(this._isDead == false){
				if(this._blood.getHealth()<=0){
					//trace("dead!");
					this._isDead = true;
					this.flag = 1;		//禁止再扔镖
					new GameOver(missionFailed,6,changeFrame);
				}
			}
			
			if(this._canMove == true){
				if(this._Zabuza){
					this._Zabuza.moving();
				}
			}
			
			//碰撞检测
			flyshittest.flysobjtesting2(this._stage,event,this._Zabuza,flystestobj,this._blood,this._appStart); //开始BOSS碰撞检测
			flyshittest.flysHurtMeTest(this._stage,event,me,flyshurtmeobj,this._blood);		//主角被飞镖伤碰撞检测
			
			if(this._SYLDwidthChange == true){
				this._aSuYiLiuDan.width += 11;	//水龙弹出击速度
				flyshittest.skillHurtMeTest(this._stage,event,me,this._skillsHurtMeObj,this._blood);	//主角被技能伤碰撞检测
			}
			
			//如果主角攻击存在且飞镖超出边界，移除忍标 
			if( meattack ){
				var flys1:Flys1 = meattack.getflys1();
				if(flys1){
					if(flag==1){
						if( (flys1.x>=this._stage.stageWidth) || (flys1.y>=this._stage.stageHeight) || (flys1.x<0) || (flys1.y<0) ){
							meattack.removeflys1();
							flys1 = null;
							meattack = null;
							this.flag = 0;
							return;
						}
					}
					var vx:Number = Math.cos(meattack.angle) * meattack.flysspeed;
					var vy:Number = Math.sin(meattack.angle) * meattack.flysspeed;
					flys1.x += vx;
					flys1.y += vy;
				}
			}
			
			//如果再不斩攻击存在且飞镖超出边界，移除忍标(再不斩的飞镖)
			if(this._Zabuza){
				if(this._Zabuza.getme()){
					if( this._Zabuza.getAttack().getflys2() ){	//如果有飞镖
						if( this._stage.contains(this._Zabuza.getAttack().getflys2()) ){	   //如果飞镖在舞台上
							var flys2 = this._Zabuza.getAttack().getflys2();
							if( (flys2.x>=this._stage.stageWidth) || (flys2.y>=this._stage.stageHeight) || (flys2.x<0) || (flys2.y<0) ){	//如果超出了边界
								this._Zabuza.getAttack().removeflys2();
							}
						}
					}
				}
			}
			
			//键盘
			for(var k=0;k<keycode.length;k++){
				if(me){
					switch(keycode[k]){
						case 87:if(me.getposy() > 50) {me.changepos("up");break};		//W
						case 83:if(me.getposy() < 770){me.changepos("down");break}; 	//S
						case 65:if(me.getposx() > 50) {me.changepos("left");break}; 	//A
						case 68:if(me.getposx() < 980){me.changepos("right");break};	//D
						
						case 48:{changeItem(0);this._status.setCurrentItem(0);break;}	//0
						case 49:{changeItem(1);this._status.setCurrentItem(1);break;}	//1
					}
				}
			}
		}
	}
}